package com.zy.baselib

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.viewbinding.ViewBinding
import com.sudworth.themvp.model.EmptyModel
import com.sudworth.themvp.model.IModel
import com.sudworth.themvp.view.AppDelegate
import com.sudworth.themvp.view.IDelegate

/**
 * 不需要使用model可以继承这个类
 * ui操作写在IDelegate
 */
abstract class BaseDiVStub<V: IDelegate>: BaseDiVMStub<V, EmptyModel>()

abstract class BaseDiStub<B: ViewBinding>: BaseDiMStub<B, EmptyModel>()

/**
 * 使用 Model,但是ui写在activity中
 */
abstract class BaseDiMStub<B: ViewBinding, M: IModel>: BaseDiVMStub<EmptyAppDelegate<B>, M>() {

    private lateinit var mViewBinding: B

    override fun canViewBinding(): Boolean {
        return true
    }

    override fun onResetContentView(): View {
        mViewBinding = onCreateVB(layoutInflater, null)
        return mViewBinding.root
    }

    abstract fun onCreateVB(inflater: LayoutInflater, container: ViewGroup?): B

    fun getViewBinding(): B {
        return mViewBinding
    }
}

/**
 * navigation 防止重复创建
 */
abstract class BaseNavDiStub<B: ViewBinding>: BaseDiVStub<EmptyAppDelegate<B>>() {

    private lateinit var mViewBinding: B
    private var hasInit = false

    override fun canViewBinding(): Boolean {
        return true
    }

    override fun onResetContentView(): View {
        mViewBinding = onCreateVB(layoutInflater, null)
        return mViewBinding.root
    }

    abstract fun onCreateVB(inflater: LayoutInflater, container: ViewGroup?): B

    fun getViewBinding(): B {
        return mViewBinding
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        hasInit = true
    }

    override fun onInitData() {
        super.onInitData()
        onNavResume(hasInit)
    }

    abstract fun onNavResume(resume: Boolean)

    override fun onDestroy() {
        hasInit = false
        super.onDestroy()
    }
}

/**
 * navigation 防止重复创建
 */
abstract class BaseNavDiVStub<V: IDelegate>: BaseDiVStub<V>() {

    private var hasInit = false

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        if(hasInit) {
            return viewDelegate.rootView
        }
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    override fun onConfigView(view: View?, savedInstanceState: Bundle?) {
        if(hasInit)return
        super.onConfigView(view, savedInstanceState)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        if(hasInit)return
        super.onViewCreated(view, savedInstanceState)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        hasInit = true
    }

    override fun onInitData() {
        if(hasInit)return
        super.onInitData()
    }

    override fun onDestroy() {
        hasInit = false
        super.onDestroy()
    }
}